package com.briup.web.controller;

import com.briup.pojo.response.Result;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author pgc
 * @Description:
 * @date 2024/11/6 9:57
 */
@RestController
public class CookieController {
@GetMapping("login2")
    public Result login2(HttpServletRequest request,
                         HttpServletResponse response){
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    System.out.println(name+":"+password);
    Cookie  cookie1=new Cookie("name",name);
    Cookie  cookie2=new Cookie("password",password);
    response.addCookie(cookie1);
    response.addCookie(cookie2);
    //默认是有有效期，有效期默认为-1，-1情况为关闭浏览器则自动清除为-1的相关cookie
    cookie1.setMaxAge(5*60);
        return Result.success();
}
@GetMapping("getCookie")
  public Result getCookie(HttpServletRequest request){
      System.out.println("cookie测试");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        String name = cookie.getName();
        String value = cookie.getValue();
        System.out.println(name+":"+value);
    }
    return Result.success();
}
}
